﻿using System;
using System.Collections.Generic;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    public class UseCasesSelectorControl : UserControlWin, IFormControl
    {
        protected BorderLayoutManager _layoutManager;
        public IMultiSelector<UseCase> UseCaseMultiSelector { get; private set; }

        private Release _release;
        //needs tests
        public Release Release
        {
            get { return _release; }
            set
            {
                _release = value;
                //UpdateState();
            }
        }

        public UseCasesSelectorControl(Drop drop)
        {
            if (drop == null) throw new ArgumentNullException("drop");

            CreateUseSelector();
            DropBeingAssigned = drop;
        }

        /// <summary>
        /// Returns the SaveButton that is used to Save the Use Cases Selected for the Drop.
        /// </summary>
        public IButton SaveButton { get; private set; }
        
     

        public UseCasesSelectorControl()
        {
            CreateUseSelector();
        }

        private void CreateUseSelector()
        {
            UseCaseMultiSelector = GlobalUIRegistry.ControlFactory.CreateMultiSelector<UseCase>();
            _buttonGroupControlWin = new ButtonGroupControlWin(GlobalUIRegistry.ControlFactory);
            SaveButton = _buttonGroupControlWin.AddButton("Save", @"&Save", ClickHandlerSave);
            _layoutManager = GlobalUIRegistry.ControlFactory.CreateBorderLayoutManager(this);
            _layoutManager.AddControl(UseCaseMultiSelector, BorderLayoutManager.Position.Centre);
            _layoutManager.AddControl(_buttonGroupControlWin, BorderLayoutManager.Position.South);
            //UpdateState();
        }

        //public void UpdateState()
        //{
        //    bool hasRelease = _release != null;
        //    ExportDropsButton.Enabled = hasRelease;
        //}

       

        private void ClickHandlerSave(object sender, EventArgs e)
        {
            foreach (UseCase useCase in UseCaseMultiSelector.Model.Added)
            {
                this.DropBeingAssigned.UseCases.Add(useCase);
            }
            foreach (var useCase in UseCaseMultiSelector.Model.Removed)
            {
                this.DropBeingAssigned.UseCases.Remove(useCase);
            }
            this.DropBeingAssigned.Save();
            this.DropBeingAssigned.UseCases.SaveAll();
        }

        private Drop _dropBeingAssigned;
        private ButtonGroupControlWin _buttonGroupControlWin;
  

        /// <summary>
        /// Gets and sets the Drop that this Multiselector is assigning use cases for.
        /// </summary>
        public Drop DropBeingAssigned
        {
            get { return _dropBeingAssigned; }
            set
            {
                _dropBeingAssigned = value;
                if (_dropBeingAssigned == null)
                {
                    UseCaseMultiSelector.AllOptions = new List<UseCase>();
                    UseCaseMultiSelector.SelectedOptions = new List<UseCase>();
                    return;
                }
                Criteria criteria1 = new Criteria("DropID", Criteria.ComparisonOp.Is, "NULL");
                Criteria criteria2 = new Criteria("DropID", Criteria.ComparisonOp.Equals, _dropBeingAssigned.DropID);
                Criteria criteria = new Criteria(criteria1, Criteria.LogicalOp.Or, criteria2);
                BusinessObjectCollection<UseCase> availableUseCases = Broker.GetBusinessObjectCollection<UseCase>
                    (criteria);
                BusinessObjectCollection<UseCase> selectedUsecases = _dropBeingAssigned.UseCases;
                UseCaseMultiSelector.AllOptions = availableUseCases;
                UseCaseMultiSelector.SelectedOptions = selectedUsecases;
            }
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form)
        {
        }
    }
}